﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.Management;
using AvatarHelper;
using Avatar.Model;

public partial class Admin_Menu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            btn_Add.Visible = true;
            panel_Add.Visible = false;
            panel_Edit.Visible = false;

            using (var _adc = DBHelper.GetDataContext())
            {
                gv_Department.DataSource = _adc.Management_Department.OrderBy(d => d.Order);
                gv_Department.DataBind();
            }
        }
    }
    protected void btn_DUpdate_Click(object sender, EventArgs e)
    {
        Button _b = sender as Button;
        int _id = Convert.ToInt32(_b.CommandArgument);
        var _d = MenuManager.GetDepartment(_id);
        if (_d != null)
        {
            txt_NameUpdate.Text = _d.Name;
            txt_Order.Text = _d.Order.ToString();
            rb_Lock.SelectedValue = _d.Open.ToString();
            btn_DUpdateSubmit.CommandArgument = _d.ID.ToString();

            btn_Add.Visible = false;
            panel_Add.Visible = false;
            panel_Edit.Visible = true;
        }
    }
    protected void btn_DAddSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (MenuManager.DepartmentAdd(txt_NameAdd.Text))
            {
                txt_NameAdd.Text = "";

                Response.Redirect(Request.Url.ToString());
            }
            else
            {
                throw new Exception("添加管理单元失败：");
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "错误:" + _e.Message;
        }
    }
    protected void btn_DDelete_Click(object sender, EventArgs e)
    {
        try
        {
            Button _b = sender as Button;
            int _id = Convert.ToInt32(_b.CommandArgument);

            MenuManager.DepartmentDelete(_id);

            Response.Redirect(Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void btn_DUpdateSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            Button _b = sender as Button;
            int _id = Convert.ToInt32(_b.CommandArgument);

            MenuManager.DepartmentUpdate(_id,
                txt_NameUpdate.Text,
                string.IsNullOrEmpty(txt_Order.Text) ? 0 : Convert.ToInt32(txt_Order.Text),
                Convert.ToBoolean(rb_Lock.SelectedValue));

            Response.Redirect(Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void btn_DUpdateCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.ToString());
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        btn_Add.Visible = false;
        panel_Add.Visible = true;
        panel_Edit.Visible = false;
    }
}
